<?php

namespace Modules\AuthAdmin\Models;
use Modules\AuthAdmin\Models\AuthGroup as AuthGroupModel;
class AuthRule extends BaseApiModel
{
	/**
     * @name   显示状态:0=隐藏,1=显示,0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getStatusAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
     * @name   验证权限:0=否,1=是,0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getAuthOpenAttribute($value)
    {
        return $value==1?true:false;
    }
	
	/**
     * @name   更新时间为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value?$value:'';
    }
	/**
     * @name   路由地址为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getHrefAttribute($value)
    {
        return $value?$value:'';
    }
	/**
     * @name   图标名称为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getIconAttribute($value)
    {
        return $value?$value:'';
    }
	/**
	 * @name  列表数据
	 * @return json 
	 */
    public function list_data($data){
		$model = $this->query();
		if(isset($data['status']) && $data['status'] == 1){
			$model = $model->whereIn('level',[1,2,3]);
			$model = $model->select('id','name','pid');
		}
        $list = $model->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->get()
				->toArray(); 
		return $this->api_success(['data'=>$this->auth_rule_arr($list)]);		
	}
	/**
	 * @name  分配权限数据
	 * @param  id  int 权限组id
	 * @return json 
	 */
    public function access($id){
		$auth_rule_arr = $this->select('id','name','pid')->orderBy('sort','asc')->orderBy('id','asc')->get()->toArray();
		$defkeys = AuthGroupModel::where(['id'=>$id])->value('rules');
		if($defkeys){
			$defkeys = $this->where(['level'=>4])->whereIn('id',explode("|", $defkeys))->pluck('id');
		}else{
			$defkeys = [];
		}
		return $this->api_success(['data'=>[
			'rule_arr'=>$this->auth_rule_arr($auth_rule_arr),
			'defkeys'=>$defkeys,
			'id'=>$id
		]]);	
	}
	/**
	 * @name  处理权限数据
	 * @param  auth_rule_arr  array 权限数据
	 * @return json 
	 */
	public function auth_rule_arr($auth_rule_arr){
		$rule_arr = [];
		$i = 0;
		foreach($auth_rule_arr as $k=>$v){
			if($v['pid'] == 0){
				$rule_arr[$i] = $v;
				unset($auth_rule_arr[$k]);
				$j = 0;
				foreach($auth_rule_arr as $k1=>$v1){
					if($rule_arr[$i]['id'] == $v1['pid']){
						$rule_arr[$i]['children'][$j] = $v1;
						unset($auth_rule_arr[$k1]);
						$s = 0;
						foreach($auth_rule_arr as $k2=>$v2){
							if($rule_arr[$i]['children'][$j]['id'] == $v2['pid']){
								$rule_arr[$i]['children'][$j]['children'][$s] = $v2;
								unset($auth_rule_arr[$k2]);
								$y = 0;
								foreach($auth_rule_arr as $k3=>$v3){
									if($rule_arr[$i]['children'][$j]['children'][$s]['id'] == $v3['pid']){
										$rule_arr[$i]['children'][$j]['children'][$s]['children'][$y] = $v3;
										unset($auth_rule_arr[$k3]);
										$y++;
									}
								}
								$s++;
							}
							
						}
						$j++;
					}
				}
				$i++;
			}
		}
		return $rule_arr;
	}
	/**
	 * @name  获取id
	 * @param id 当前删除数据id
	 * @return 返回获取当前的删除id的其他子id
	 */
	public function ids($id){
		$rule = $this->select('id','pid')->get()->toArray();
		$arr = $this->delsort($rule,$id);
		$arr[] = $id;
		return $arr;
	}
	/**
	 * @name  递归遍历数据
	 * @param cate 全部数据
	 * @param id 当前删除数据id
	 * @return 返回获取当前的删除id的其他子id
	 */ 
	private function delsort($rule,$id){
		//创建新数组
		static $arr=array();
		foreach($rule as $k=>$v){
			if($v['pid'] == $id){
				$arr[] = $v['id'];
				unset($rule[$k]);
				$this->delsort($rule,$v['id']);
			}
		}
		return $arr;
	}
}
